import json
import sys

import requests
import bs4




# 1.获取HTML数据
def get_html(url):
    res_html = requests.get(url=url, timeout=300)
    res_html.encoding=res_html.apparent_encoding.encode('utf-8')
    res_soup = bs4.BeautifulSoup(res_html.text, 'html.parser')
    return res_soup

# 2.处理HTML数据
def manager_html(url):
    souplist=get_html(url).find('ul', id='post_container').findAll('li')
    datalist=[]
    for i in souplist:
        dataitem={}
        dataitem['url']=i.find('a',class_='zoom')['href'].replace('.html','')
        dataitem['title']=i.find('h2').find('a').text
        dataitem['img']=i.find('img')['src']
        datalist.append(dataitem)
    #print(type(datalist))
    return datalist


def strTojson(url):
    str=manager_html(url)
    data=json.dumps(str,ensure_ascii=True)
    print(data)
    return data


def main():
    if sys.argv[1]=="1":
        # print(type(sys.argv[1]))
        url = 'https://www.66s6.net/'
        strTojson(url)
    else:
        url = "https://www.66s6.net/index_" + sys.argv[1] + ".html"
        strTojson(url)

if __name__ == '__main__':
    # "https://www.66s6.net/index_2.html"
    main()
    
